package com.xbn.xbnmall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xbn.common.valid.AddGroup;
import com.xbn.common.valid.UpdateGroup;
import com.xbn.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.xbn.xbnmall.product.entity.BrandEntity;
import com.xbn.xbnmall.product.service.BrandService;
import com.xbn.common.utils.PageUtils;
import com.xbn.common.utils.R;


/**
 * 品牌
 *
 * @author XBN
 * @email 78742541@qq.com
 * @date 2020-06-26 15:16:04
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;


    /*
    *
    * */
    @GetMapping("/infos")
    public R infos(@RequestParam("brandIds")List<Long> brandIds){
       List<BrandEntity> brandEntities=  brandService.getBrandByIds(brandIds);

       return R.ok().put("brand",brandEntities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
   // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:brand:save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand/*, BindingResult result*/){
        //如果校验出错
    /*    if(result.hasErrors())
        {
            Map<String,String> errors=new HashMap<>();
             result.getFieldErrors().forEach((item)->{
                 //获取错误提示
                 String message=item.getDefaultMessage();
                 String field=item.getField();
                 errors.put(field,message);
             });
            return R.error(400,"提交数据不合法:").put("data",errors);
        }*/
		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     * 修改的时候要更新其它表的冗余数据
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:brand:update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }
    /**
     * 修改showStatus状态
     */
    @RequestMapping("/update/status")
    //  @RequiresPermissions("product:brand:update")
    public R updateStutus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
 //   @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
